using System.Text.Json;
using System.Text.Json.Serialization;

namespace CoreManager.Core.Extensions
{
    /// <summary>
    /// 自定义DateTime JSON转换器
    /// 统一输出格式为: yyyy-MM-dd HH:mm:ss
    /// </summary>
    public class DateTimeJsonConverter : JsonConverter<DateTime>
    {
        private const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

        public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType == JsonTokenType.String)
            {
                var dateString = reader.GetString();
                if (DateTime.TryParse(dateString, out var dateTime))
                {
                    return dateTime;
                }
            }

            throw new JsonException($"Unable to convert \"{reader.GetString()}\" to DateTime.");
        }

        public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
        {
            writer.WriteStringValue(value.ToString(DateTimeFormat));
        }
    }

    /// <summary>
    /// 可空DateTime JSON转换器
    /// </summary>
    public class NullableDateTimeJsonConverter : JsonConverter<DateTime?>
    {
        private const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

        public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType == JsonTokenType.Null)
            {
                return null;
            }

            if (reader.TokenType == JsonTokenType.String)
            {
                var dateString = reader.GetString();
                if (string.IsNullOrEmpty(dateString))
                {
                    return null;
                }

                if (DateTime.TryParse(dateString, out var dateTime))
                {
                    return dateTime;
                }
            }

            throw new JsonException($"Unable to convert \"{reader.GetString()}\" to DateTime?.");
        }

        public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerializerOptions options)
        {
            if (value.HasValue)
            {
                writer.WriteStringValue(value.Value.ToString(DateTimeFormat));
            }
            else
            {
                writer.WriteNullValue();
            }
        }
    }

    /// <summary>
    /// DateTimeOffset JSON转换器
    /// </summary>
    public class DateTimeOffsetJsonConverter : JsonConverter<DateTimeOffset>
    {
        private const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

        public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType == JsonTokenType.String)
            {
                var dateString = reader.GetString();
                if (DateTimeOffset.TryParse(dateString, out var dateTimeOffset))
                {
                    return dateTimeOffset;
                }
            }

            throw new JsonException($"Unable to convert \"{reader.GetString()}\" to DateTimeOffset.");
        }

        public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)
        {
            writer.WriteStringValue(value.ToString(DateTimeFormat));
        }
    }

    /// <summary>
    /// 可空DateTimeOffset JSON转换器
    /// </summary>
    public class NullableDateTimeOffsetJsonConverter : JsonConverter<DateTimeOffset?>
    {
        private const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

        public override DateTimeOffset? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType == JsonTokenType.Null)
            {
                return null;
            }

            if (reader.TokenType == JsonTokenType.String)
            {
                var dateString = reader.GetString();
                if (string.IsNullOrEmpty(dateString))
                {
                    return null;
                }

                if (DateTimeOffset.TryParse(dateString, out var dateTimeOffset))
                {
                    return dateTimeOffset;
                }
            }

            throw new JsonException($"Unable to convert \"{reader.GetString()}\" to DateTimeOffset?.");
        }

        public override void Write(Utf8JsonWriter writer, DateTimeOffset? value, JsonSerializerOptions options)
        {
            if (value.HasValue)
            {
                writer.WriteStringValue(value.Value.ToString(DateTimeFormat));
            }
            else
            {
                writer.WriteNullValue();
            }
        }
    }
}
